// Binary search on a 1D array ... 2D array still in progress

	#include<stdio.h>
	#include<conio.h>


	int twoo = 0, size2 = 0;
	void main()
	{
	  int oned[250], twod[50][50], twodsort[250];
	  int i, j, temp, ch, rows, cols;

	  void oneD(int oned[]);
	  void twoD(int oned[], int rows, int cols, int size2);

	  clrscr();

	  gotoxy(10, 2);
	  printf("Search a 1-D or 2-D array ? ");
	  scanf("%d", &ch);

	  switch(ch)
	  {

	    case 1 : oneD(oned); break;

	    case 2 : printf("\nEnter the no of rows : ");
		     scanf("%d", &rows);

		     printf("\nEnter the cols : ");
		     scanf("%d", &cols);

		     for(i = 0;i < rows;i ++)
		     {
		       for(j = 0;j < cols;j ++)
		       {
			printf("\n Enter element [%d][%d] : ", i, j);
			scanf("%d", twod[i][j]);
		       }
		     }

		     size2 = rows * cols;

		     int put = 0;
		     for(i = 0;i < rows;i ++)
		     {
		       for(j = 0;j < cols;j ++)
		       {
			 oned[put] = twod[i][j];
			 put ++;
		       }
		     }

		     twoD(oned, rows, cols, size2);
	  }

	  getch();
	}

	void oneD(int oned[])
	{
	  int i, j, temp, size;

	  clrscr();

	  gotoxy(20,3);
	  printf("Enter the array size ? ");
	  scanf("%d", &size);


	  clrscr();

	  for(i = 0;i < size;i ++)
	  {

	    gotoxy(20,3);
	    printf("Enter the value of element [%d] : ", (i+1));
	    scanf("%d", &oned[i]);
	    textbackground(BLACK);

	    gotoxy(54, 3);
	    cprintf("\b        ");
	  }

	  for(i = 0;i < size;i ++)
	   for(j = 0;j < size - 1 - i;j ++)
	   {

	    if(oned[j] > oned[j + 1])
	    {
	      temp = oned[j];
	      oned[j] = oned[j + 1];
	      oned[j + 1] = temp;
	    }
	   }


	  gotoxy(20,3);
	  printf("\n Sorted array is  :" );
	  for(i = 0;i < size;i ++)
	   printf("%d, ",oned[i]);

	 //Binary Search

	  gotoxy(20, 5);


	  int element, brkpt = 0, low, up, mid = 0;
	  low = 0;
	  up = (size - 1);

	  printf("Enter the elem to be searched : ");
	  scanf("%d", &element);

	  while(low <= up)
	  {
	    mid = (low + up) / 2;

	    if(element > oned[mid])
	     low = mid + 1;

	    else if(element < oned[mid])
	     up = mid - 1;

	    else
	    {
	      brkpt = 1; break;
	    }
	  }


	  gotoxy(10, 10);

	  if(brkpt == 1)
	   printf("Elemet present at position : \" %d \" in the above sorted list ", (mid + 1));

	  else
	   printf("Sorry Element not present ");


	}

	void twoD(int oned[], int rows, int cols, int size2)
	{

	   int temp, temp1;
	   clrscr();
	   printf("rows cols size are  : %d   %d   %d\n" , rows, cols, size2);

	   for(temp = 0;temp < size2; temp++)
	    printf("%d    ", oned[temp]);

	   getch();

	   for(int i = 0;i < size2;i ++)
	    for(int j = 0;j < size2 - 1 - i;j ++)
	    {

	     if(oned[j] > oned[j + 1])
	     {
	       temp = oned[j];
	       oned[j] = oned[j + 1];
	       oned[j + 1] = temp;
	     }
	    }


	    gotoxy(20,3);
	    printf("\n Sorted array is  :" );
	    for(i = 0;i < size2;i ++)
	     printf("%d, ",oned[i]);

	    gotoxy(30, 4);
	    int put = 0;
	    for(i = 0;i < rows;i ++)
	     {
	      for(j = 0;j < cols;j ++)
	      {
		printf("%d\t", oned[put]);
		put ++;
	      }
	      gotoxy(30, (4 + i + 1));
	     }
	  //Binary Search

	    gotoxy(20, 8);


	    int element, brkpt = 0, low, up, mid = 0;
	    low = 0;
	    up = (size2 - 1);

	    printf("Enter the elem to be searched : ");
	    scanf("%d", &element);

	    while(low <= up)
	    {
	      mid = (low + up) / 2;

	      if(element > oned[mid])
	       low = mid + 1;

	      else if(element < oned[mid])
	       up = mid - 1;

	      else
	      {
		brkpt = 1; break;
	      }
	    }

	    if(brkpt == 1)
	     printf("Elemet present at position : \" %d \" in the above sorted list ", (mid + 1));

	    else
	     printf("Sorry Element not present ");

	}